package mylib.dir;

/**
 * Created with IntelliJ IDEA.
 * User: 1
 * Date: 19.08.12
 * Time: 16:18
 * To change this template use File | Settings | File Templates.
 */
public class Complex {
    private double real, imaginary;

    public Complex(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public Complex(double real) {
        this.real = real;
        this.imaginary = 0;
    }

    public Complex add(Complex other) {
        return new Complex(real + other.real, imaginary + other.imaginary);
    }

    public Complex subtract(Complex other) {
        return new Complex(real - other.real, imaginary - other.imaginary);
    }

    public Complex multiply(Complex other) {
        return new Complex(real * other.real - imaginary * other.imaginary,
                real * other.imaginary + imaginary * other.real);
    }

    public Complex divide(Complex other) {
        // (a+bi)*(c-di) / (c+di)*(c-di) =
        // (a*c+b*d) + (b*c-a*d) * i
        // c*c+d*d
        double den = other.real * other.real + other.imaginary * other.imaginary;
        return new Complex((real * other.real + imaginary * other.imaginary) / den,
                (imaginary * other.real - real * other.imaginary) / den);
    }

    @Override
    public String toString() {
        return real + "+" + imaginary + "*i";
    }

    public double getReal() {
        return real;
    }

    public void setReal(double real) {
        this.real = real;
    }

    public double getImaginary() {
        return imaginary;
    }

    public void setImaginary(double imaginary) {
        this.imaginary = imaginary;
    }
}
